/**
 * 登录
 * @author tiedong
 * @since 2024/02/22
 */
layui.use(['index','treeTable','form'], function () {
    var $ = layui.jquery;
    var treeTable = layui.treeTable;
    var form = layui.form

    var getList = '/admin/access/getlist'; //获取列表数据接口
    var addUrl = '/admin/access/add';
    var editUrl = '/admin/access/edit';
    var delUrl = '/admin/access/del';

    var inst = treeTable.render({
        elem: '#table-index',
        url: getList+"?type=",
        // data: createData(!0),
        maxHeight: 'full-50',
        toolbar: '#toolbar',
        tree: {
            customName: {
                name: 'name'
            },
            view: {
                iconLeaf: '<i class="layui-icon layui-icon-list"></i>'
            }
        },
        cols: [[
            {field: 'id', title: 'ID', width: 80, sort: true},
            {field: 'name', title: '菜单名称', width: 220},
            {field: 'icon', title: '图标', width: 100,templet: function (d) {
                    return '<i class="layui-icon '+d.vicon+'"></i>'
             }},
            {field: 'url', title: 'URL地址', width: 180},
            {field: 'type', title: '类型', width: 100,templet: function (d) {
                    let strs = ['<span class="layui-badge layui-bg-blue">菜单</span>',
                        '<span class="layui-badge layui-bg-cyan">接口</span>',
                        '<span class="layui-badge layui-bg-gray">按钮</span>',
                    ];
                    return strs[d.type-1];
                }},
            {field: 'sort', title: '排序', width: 80},
            { fixed: "right", title: "操作", minWidth: 150,  toolbar: "#bar-handle"}
        ]],
        page: false,
    });

    // 展开所有
    $('#expandAll').click(function(){
        treeTable.expandAll('table-index', true);
    })

    // 折叠所有
    $('#foldAll').click(function () {
        treeTable.expandAll('table-index',false);
    });


    // 搜索提交
    form.on('submit(search)', function(data){
        var field = data.field; // 获得表单字段
        console.log("field",field)
        // 执行搜索重载
        treeTable.reload('table-index', {
            url:getList,
            page:false,
            where: field // 搜索的字段
        });
        return false; // 阻止默认 form 跳转

    });

    // 工具栏事件
    treeTable.on('toolbar(table-index)', function (obj) {
        var id = obj.config.id;
        var checkStatus = treeTable.checkStatus(id);
        var othis = lay(this);
        switch (obj.event) {
            case 'add':
                active[obj.event] ? active[obj.event]("添加菜单"): '';
                break;
        }
    });

    //触发单元格工具事件
    treeTable.on('tool(table-index)', function (obj) {
        var data = obj.data;
        console.log(obj)
        switch (obj.event) {
            case 'edit':
                active[obj.event] ? active[obj.event]("编辑菜单",data): '';
                break;
            case 'del':
                layer.confirm('是否真的删除数据', function () {
                    active[obj.event] ? active[obj.event](data.id): '';
                });
                break;
        }
    });

    /* 事件-指定开关 */
    form.on('switch(status-switch)', function(obj){
        active["status"] ? active["status"](obj): '';
    });

    //事件处理
    var active = {
        //添加
        add:function (title){
            layer.open({
                type: 2
                ,title: title
                ,content: addUrl
                ,maxmin: true
                ,area: ['550px', '670px']
                ,btn: ['确定', '取消']
                ,yes: function(index, layero){
                    //点击确认触发 iframe 内容中的按钮提交
                    var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                    submit.click();
                }
            });
        },
        //编辑
        edit:function (title,data){
            json = JSON.stringify(data);
            layer.open({
                type: 2
                ,title: title
                ,content: editUrl+'?id='+data.id
                ,maxmin: true
                ,area: ['550px', '670px']
                ,btn: ['确定', '取消']
                ,yes: function(index, layero){
                    //点击确认触发 iframe 内容中的按钮提交
                    var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                    submit.click();
                }
            });
        },
        //删除
        del:function (id) {
            $.ajax({
                url:delUrl+'',
                method: "GET",
                data: {id:id},
                success: function(res) {
                    if(res.code == 0){
                        layer.msg(res.msg, {
                            icon: 1,
                            time: 2000,
                        },function () {
                            window.location.reload()
                        });
                    } else {
                        layer.msg(res.msg, {
                            icon: 5,
                            time: 1500
                        });
                    }
                },
                error: function(e) {
                    layer.msg("删除失败");
                }
            });
        }
    }

});